import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public abstract class Animal extends Component implements Runnable {
	protected BufferedImage[] iconsRight, iconsLeft, icons;
	protected BufferedImage currentIcon;
	protected int xpos, ypos, width, height, current;
	protected String name, orientation;
	protected boolean dead;

	public static final int MOVE = 0;
	public static final int SHOT = 1;
	public static final int DEAD = 2;
	
	public Animal(int x, int y, String name, String orientation) {
		this.xpos = x;
		this.ypos = y;
		this.current = 0;
		this.name = name;
		this.orientation = orientation;
		this.iconsRight = new BufferedImage[3];
		this.iconsLeft = new BufferedImage[3];
		this.dead = false;
		try {
			iconsLeft[0] = ImageIO.read(new File("resources/"+name+"_moving_left.png"));
			iconsRight[0] = ImageIO.read(new File("resources/"+name+"_moving_right.png"));
			iconsLeft[1] = ImageIO.read(new File("resources/"+name+"_shot_left.png"));
			iconsRight[1] = ImageIO.read(new File("resources/"+name+"_shot_right.png"));
			iconsLeft[2] = ImageIO.read(new File("resources/"+name+"_dead_left.png"));
			iconsRight[2] = ImageIO.read(new File("resources/"+name+"_dead_right.png"));
			if (orientation.equals("left")) {
				icons = iconsLeft;
				currentIcon = icons[MOVE];				
			}
			else if (orientation.equals("right")) {
				icons = iconsRight;
				currentIcon = icons[MOVE];	
			}
			this.width = currentIcon.getWidth();
			this.height = currentIcon.getHeight();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// loop for setting transparent pixels, could possibly be made useless
		// if we can find a way to
		// automatically encode images with proper alpha settings.
		for(int i=0; i<icons.length; i++) {
			BufferedImage temp = icons[i];
			int h = temp.getHeight();
			int w = temp.getWidth();
			for (int y2 = 0; y2 < h; y2++) {
				for (int x2 = 0; x2 < w; x2++) {
					int argb = temp.getRGB(x2, y2);
					if ((argb & 0x00FFFFFF) == 0x00000000) {
						temp.setRGB(x2, y2, 0);
					}
				}
			}
			
		}
		
		

	}
	
	public void kill() {
		dead = true;	
		currentIcon = icons[SHOT];	
	}
	
	public boolean isDead() {
		return dead;
	}
	
	public void setDead(boolean bool) {
		this.dead = bool;
	}

	public boolean containsPoint(int x, int y) {
		if (inXRange(x) && inYRange(y))
			return true;
		else
			return false;
	}

	public BufferedImage getCurrentIcon() {
		return currentIcon;
	}
	
	public void setCurrentIcon(int a) {
		currentIcon = icons[a];
	}
	
	public String getName() {
		return name;
	}


	public int getXPos() {
		return xpos;
	}

	public int getYPos() {
		return ypos;
	}

	public int getX2() {
		return xpos + width;
	}

	public int getY2() {
		return ypos + height;
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}

	public abstract void move(); 

	public void paint(Graphics2D g) {
		g.drawImage(currentIcon, null, xpos, ypos);
		//g.drawImage(image, xpos, ypos, width, height, null);
	}

	private boolean inXRange(int x) {
		if (x >= xpos && x <= (xpos + width))
			return true;
		else
			return false;
	}

	private boolean inYRange(int y) {
		if (y >= ypos && y <= (ypos + height))
			return true;
		else
			return false;
	}
	
	 private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2)
	  {
	    // Primitive test, just an example
	    final int r1 = c1.getRed();
	    final int g1 = c1.getGreen();
	    final int b1 = c1.getBlue();
	    final int r2 = c2.getRed();
	    final int g2 = c2.getGreen();
	    final int b2 = c2.getBlue();
	    ImageFilter filter = new RGBImageFilter()
	    {
	      public final int filterRGB(int x, int y, int rgb)
	      {
	        int r = (rgb & 0xFF0000) >> 16;
	        int g = (rgb & 0xFF00) >> 8;
	        int b = rgb & 0xFF;
	        if (r >= r1 && r <= r2 &&
	            g >= g1 && g <= g2 &&
	            b >= b1 && b <= b2)
	        {
	          // Set fully transparent but keep color
	          return rgb & 0xFFFFFF;
	        }
	        return rgb;
	      }
	    };

	    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
	    return Toolkit.getDefaultToolkit().createImage(ip);
	  }
}
